package vip.xiaonuo.modular.jflow.controller;

import bp.da.DataType;
import bp.difference.SystemConfig;
import bp.difference.handler.CommonUtils;
import bp.difference.handler.HttpHandlerBase;
import bp.web.WebUser;
import bp.wf.httphandler.WF_Comm;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.support.DefaultMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;

/**
 * JFlow控制器
 *
 * @author jiangcs
 * @since 2024/01/24
 */
@RestController
@RequestMapping("/WF/Comm")
public class JFlowCommController extends HttpHandlerBase {
    /**
     * 默认执行的方法
     *
     * @return
     */
    @RequestMapping(value = "/ProcessRequest")
    public final void ProcessRequest(HttpServletRequest request) throws Exception {
        SystemConfig.setIsBSsystem(true);
        //处理Token信息
        DealToken();
        WF_Comm CommHandler = new WF_Comm();
        if (request instanceof DefaultMultipartHttpServletRequest) {
            //如果是附件上传Request，则将该Request放入全局Request。为了解决springmvc中全局Request无法转化为附件Request
            HttpServletRequest request1 = CommonUtils.getRequest();
            request1.setAttribute("multipartRequest", request);
        }
        super.ProcessRequestPost(CommHandler);
    }

    @Override
    public Class<WF_Comm> getCtrlType() {
        return WF_Comm.class;
    }

    private void DealToken() throws Exception {
        String doMethod = this.GetRequestVal("DoMethod");
        String doType = this.getDoType();
        if (doType.equals("HttpHandler"))
            doType = doMethod;
        if (DataType.IsNullOrEmpty(doType))
            throw new RuntimeException("err@没有获取到执行的方法");

        doType = doType.toLowerCase();

        if (doType.contains("login")
                || doType.contains("index")
                || doType.contains("selectoneorg_init")
                || doType.contains("getorgbyno")
                || doType.contains("admin")
                || doType.contains("dbinstall")
                || doType.contains("default_logout")
                || doType.contains("checkencryptenable")
                || doType.contains("ccbpmservices")
                || doType.contains("pcandmobileurl")
                || doType.contains("do_init")
                || doType.contains("portsaas_init"))
            return;
        String token = this.GetRequestVal("Token");
        if (!DataType.IsNullOrEmpty(token) && (token.equals("undefined") || token.equals("null")))
            token = "";
        if (!DataType.IsNullOrEmpty(token)) {
            bp.wf.Dev2Interface.Port_LoginByToken(token);
            return;
        }
        if (DataType.IsNullOrEmpty(WebUser.getNo())) {
            throw new Exception("err@登录信息丢失，请重新登录");
        }
    }
}
